using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using JigLibX.Physics;
using Utilities;
using SpaceBattle.Logic;
using SpaceBattle.Logic.SampleLevel;


namespace SpaceBattle
{
    using Nuke = Projectile;
    using NukeData = ProjectileData;

    public class OverlaysLayer : AsteroidsLayer
    {
        public OverlaysLayer(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
        }

        protected override void DrawBonuses(State current_state)
        {
            //for_each (var bonus in current_state.bonuses)
            for (int i = 0; i < current_state.bonuses.Count; i++)
            {
                var bonus = current_state.bonuses[i];
                var sp = GraphicsDevice.Viewport.Project(bonus.geometry.Position, projection, view, Matrix.Identity);
                var sp_right = GraphicsDevice.Viewport.Project(Vector3.Left * bonus.geometry.bounding_sphere().Radius,
                  projection, view, Matrix.Identity);
                var sp_left = GraphicsDevice.Viewport.Project(Vector3.Right * bonus.geometry.bounding_sphere().Radius,
                  projection, view, Matrix.Identity);
                var gift_size = (int)Vector3.Distance(sp_right, sp_left);
                spriteBatch.Draw(gift,
                    new Rectangle((int)(sp.X - gift_size / 2), (int)(sp.Y - gift_size / 2), gift_size, gift_size),
                    Color.White);
                var bs = bonus.geometry.bounding_sphere();
                bs.Center += bonus.geometry.Position;
            }
        }

        Color green_life = new Color(18, 255, 0);
        protected override void DrawShipsStats(GameTime gameTime, State current_state, float dt, ArrayList<Ship> enemies, ModelWithVolumes Player)
        {
            for (int i = 0; i < enemies.Count; i++)
            {
                var ship = enemies[i];
                var model = this[ship.ship_type];
                var world = model.World(ship.geometry);
                
                var sp = GraphicsDevice.Viewport.Project(model.BoundingSphere.Center, projection, view, world);
                var sp_right = GraphicsDevice.Viewport.Project(Vector3.Left * model.BoundingSphere.Radius * 1.3f, projection, view, world);
                var sp_left = GraphicsDevice.Viewport.Project(Vector3.Right * model.BoundingSphere.Radius * 1.3f, projection, view, world);

                var cursor_size = Vector3.Distance(sp_right, sp_left);
                if (ship.ship_type == ShipType.Boss1 || ship.ship_type == ShipType.Boss2 || ship.ship_type == ShipType.Boss3 ||
                    ship.ship_type == ShipType.Boss4 || ship.ship_type == ShipType.Boss5)

                    spriteBatch.Draw(thick_cursor, new Vector2(sp.X, sp.Y), null, Color.Lerp(Color.Red, green_life, ship.stats.CurrentLife / ship.stats.MaxLife),
                  (float)gameTime.TotalGameTime.TotalSeconds * 0.5f, new Vector2(thick_cursor.Width, thick_cursor.Height) * 0.5f, cursor_size / thick_cursor.Width*2, SpriteEffects.None, 0.0f);
                else
                    spriteBatch.Draw(thick_cursor, new Vector2(sp.X, sp.Y), null, Color.Lerp(Color.Red, green_life, ship.stats.CurrentLife / ship.stats.MaxLife),
                  (float)gameTime.TotalGameTime.TotalSeconds * 0.5f, new Vector2(thick_cursor.Width, thick_cursor.Height) * 0.5f, cursor_size / thick_cursor.Width, SpriteEffects.None, 0.0f);
                
                
            }

            //DrawCapitalShipStats(Player, current_state.player.AsShip, current_state.player.stats, dt);
            //DrawCapitalShipShield(Player, current_state.player, dt);

            //for (int i = 0; i < current_state.secondary_weapons.Count; i++)
            //{
            //DrawNukeStats(Nuke, current_state.secondary_weapons[i], dt);
            //}

            if (current_state.player.waypoint.HasValue)
            {
                var sp = GraphicsDevice.Viewport.Project(current_state.player.waypoint.Position, projection, view, Matrix.Identity);
                var cursor_size = 30.0f;
                spriteBatch.Draw(thick_cursor, new Vector2(sp.X, sp.Y), null, Color.CornflowerBlue,
                  (float)gameTime.TotalGameTime.TotalSeconds * 0.5f, new Vector2(thick_cursor.Width, thick_cursor.Height) * 0.5f, cursor_size / thick_cursor.Width, SpriteEffects.None, 0.0f);
                cursor_size = 25.0f;
                spriteBatch.Draw(thick_cursor, new Vector2(sp.X, sp.Y), null, Color.White,
                  -(float)gameTime.TotalGameTime.TotalSeconds, new Vector2(thick_cursor.Width, thick_cursor.Height) * 0.5f, cursor_size / thick_cursor.Width, SpriteEffects.None, 0.0f);
            }
        }



    }
}
